﻿using System;
using System.Collections.Generic;
using System.Text;
using Telerik.Web.UI;
using System.Web.UI;
using System.Linq;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    public class RadGridHyperLinkColumn : GridColumn
    {


        /*public override void PrepareCell(System.Web.UI.WebControls.TableCell cell, GridItem item)
        {
            base.PrepareCell(cell, item);

            if (!base.DesignMode && item is GridDataItem)
            {
                var links = cell.Controls.OfType<HyperLink>().ToArray();
                if(!string.IsNullOrEmpty(LinkCssClass))
                    links.Select(x => x.CssClass = LinkCssClass);
                foreach (var link in links)
                {
                    if (item is GridDataInsertItem)
                    {
                        link.Visible = ShowInInsertMode;
                    }
                    else if (((GridDataItem)item).IsInEditMode)
                    {
                        link.Visible = ShowInEditMode;
                    }
                }
            }
        }*/

        public bool ShowInEditMode{get;set;}
        public bool ShowInInsertMode { get; set; }
        public string DataTextField { get; set; }
        public string DataTextFormat { get; set; }
        public string Text { get; set; }
        public string DataNavigateUrlFormatString { get; set; }
        public string DataNavigateUrlFields { get; set; }
        public string LinkCssClass { get; set; }
        public string NavigateUrl { get; set; }
        public string Target { get; set; }

        /*public override System.Collections.IDictionary GetCustomPropertyDataFields(object dataItemInstance)
        {
            return base.GetCustomPropertyDataFields(dataItemInstance);
        }

        protected override string FormatDataNavigateUrlValue(object[] dataUrlValues)
        {
            //return string.Format(this.DataNavigateUrlFormatString, dataUrlValues);
            return base.FormatDataNavigateUrlValue(dataUrlValues);
        }*/

    
        public override GridColumn  Clone()
        {
            return new RadGridHyperLinkColumn();
        }

        public override void InitializeCell(TableCell cell, int columnIndex, GridItem inItem)
        {
            if (inItem is GridDataItem)
            {
                bool display = true;
                if (inItem is GridDataInsertItem && !ShowInInsertMode)
                {
                    display = false;
                }
                else if (inItem.IsInEditMode && !ShowInEditMode)
                {
                    display = false;
                }

                if (display)
                {
                    Hyperlink lnk = new Hyperlink();
                    lnk.ID = string.Concat(this.UniqueName, "_lnk");
                    lnk.DataBinding += new EventHandler(lnk_DataBinding);
                    cell.Controls.Add(lnk);
                }
            }
            else
            {
                base.InitializeCell(cell, columnIndex, inItem);
            }
        }

        void lnk_DataBinding(object sender, EventArgs e)
        {
            Hyperlink lnk = sender as Hyperlink;
            Telerik.Web.UI.GridDataItem gridDataItem = lnk.NamingContainer as Telerik.Web.UI.GridDataItem;
            if (gridDataItem != null)
            {
                if (!string.IsNullOrEmpty(DataTextField))
                {
                    lnk.Text = EvalDataFormat(DataTextFormat, DataTextField, gridDataItem.DataItem);
                }
                else if(!string.IsNullOrEmpty(Text))
                {
                    lnk.Text = Text;
                }
                if (!string.IsNullOrEmpty(DataNavigateUrlFields))
                {
                    lnk.NavigateUrl = EvalDataFormat(DataNavigateUrlFormatString, DataNavigateUrlFields, gridDataItem.DataItem);
                }
                else if (!string.IsNullOrEmpty(NavigateUrl))
                {
                    lnk.NavigateUrl = NavigateUrl;
                }
                if (!string.IsNullOrEmpty(Target))
                {
                    lnk.Target = Target;
                }
            }
        }

        private string EvalDataFormat(string formatString, string dataFields, object data)
        {
            var fields = dataFields.Split(',');
            var objects = fields.Select(x => DataBinder.Eval(data, x.Trim())).ToArray();
            if (formatString == null)
            {
                //if null create list eg "{0} {1} {2}"
                StringBuilder tmp = new StringBuilder();
                for(var i = 0; i < fields.Length; i++)
                {
                    if (i > 0)
                        tmp.Append(" ");
                    tmp.AppendFormat("{{{0}}}", i);
                }
                formatString = tmp.ToString();
            }
            return string.Format(formatString, objects);
        }

    }
}
